#!/usr/bin/perl
#save_ftp.cgi

require './protocolman-lib.pl';

&ReadParse();

my (%gbconfig, $i, $sharepath, %sharetab, $err_msg, $bw_unit);

&foreign_require("pureftpd", "pureftpd-lib.pl");
&foreign_call("pureftpd", "get_global_config", \%gbconfig);

$gbconfig{'MaxClientsNumber'} = $in{"maxclient"};
$gbconfig{'MaxClientsPerIP'} = $in{"client_per_ip"};
$gbconfig{'PerUserLimits'} =~ s/:.*$/:$in{"anonyclient"}/;
$gbconfig{'AnonymousBandwidth'} = $in{"anony_bw"};
$gbconfig{'Bind'} = $in{'portno'};
$bw_unit = "KB" if($in{"anony_bw"} == 0);
$bw_unit = "MB" if($in{"anony_bw"} == 1);

&error_setup($text{'save_ftp_error'});

&foreign_require("shareman", "shareman-lib.pl");
$err_msg = &foreign_call("shareman", "check_input_not_integer", $in{"maxclient"});
if($err_msg)
{
	$err_msg = $text{'err_msg_maxclient'};
	&nas_syslog("protocolman", "naslog_warning", $text{'naslog_ev_saveftppt_maxclient'}, $text{'naslog_msg_saveftppt_maxclient'});
	&error($err_msg);
}

$err_msg = &foreign_call("shareman", "check_input_not_integer", $in{"client_per_ip"});
if(($err_msg) || ($in{'portno'} == 0))
{
	$err_msg = $text{'err_msg_clientperip'};
	&nas_syslog("protocolman", "naslog_warning", $text{'naslog_ev_saveftppt_clientperip'}, $text{'naslog_msg_saveftppt_clientperip'});
	&error($err_msg);
}

$err_msg = &foreign_call("shareman", "check_input_not_integer", $in{'portno'});
if($err_msg){
	$err_msg = $text{'err_msg_port'};
	&error($err_msg);
}
$err_msg = &foreign_call("shareman", "check_input_not_integer", $in{"anonyclient"}) if ($in{"anonyclient"});
if($err_msg)
{
	$err_msg = $text{'err_msg_anonyclient'};
	&nas_syslog("protocolman", "naslog_warning", $text{'naslog_ev_saveftppt_anonyclient'}, $text{'naslog_msg_saveftppt_anonyclient'});
	&error($err_msg);
}

$err_msg = &foreign_call("shareman", "check_input_num", $in{"anony_bw"}, $bw_unit) if ($in{"anony_bw"});
if($err_msg)
{
	$err_msg = $text{'err_msg_anonybw'};
	&nas_syslog("protocolman", "naslog_warning", $text{'naslog_ev_saveftppt_anonybw'}, $text{'naslog_msg_saveftppt_anonybw'});
	&error($err_msg);
}

if($in{"bw_unit"})
{
	$gbconfig{'AnonymousBandwidth'} *= 1024;	
}
$gbconfig{'AnonymousBandwidth'} = sprintf "%.0f" , $gbconfig{'AnonymousBandwidth'};

&foreign_require("shareman", "shareman-lib.pl");
&foreign_call("shareman", "read_sharetab", \%sharetab, 1);   #sharename is key
$sharepath = $sharetab{$in{'sharename'}}->{"sharepath"};
unless(($sharepath =~ /^\/usrvol\/volume/))
{
	$sharepath = "";
}


&foreign_require("pureftpd", "pureftpd-lib.pl");
&foreign_call("pureftpd", "set_anonymous_home", $sharepath);
&foreign_call("pureftpd", "set_global_config", \%gbconfig);
`$config{'ftp_service'} restart`;
&nas_syslog("protocolman", "naslog_info", $text{'naslog_ev_saveftppt_succ'}, $text{'naslog_msg_saveftppt_succ'});

&redirect("index.cgi");
